using System;
using System.IO;

namespace NCover.DeinstrumentMethods
{
	using Objects;
	using Utilities;

	/// <summary>
	/// Undoes instrumentation by saving as a different file extension.
	/// E.g. .cs.uninstrumented.
	/// </summary>
	public class DifferentExtension : DeinstrumentMethod
	{
		private string _extention = ".instrumented";
		private string _shouldntExistExtention = ".createdByInstrumentation";

		#region DeinstrumentMethod Members

		public void preinstrument(string filename)
		{
			if (File.Exists(filename) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53) && NCover.NCoverCheck.Add(53))
			{
				if (!File.Exists(filename + _extention) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54) && NCover.NCoverCheck.Add(54))
				{
					File.Copy(filename, filename + _extention, false);
					//remove readonly attribute if nessesary (E.g. sourcesafe non checked out)
					FileAttributes attrs = File.GetAttributes(filename);
					if ((attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55) && NCover.NCoverCheck.Add(55))
					{
						//attrs = FileAttributes.Normal;
						attrs = (FileAttributes) ((int)attrs) - ((int)FileAttributes.ReadOnly);
						File.SetAttributes(filename, attrs);
					}
				}
			}
			else
			{
				//record file's non-existance
				new TextFile(filename + _shouldntExistExtention).Text = "File " + filename + " was only created for instrumentation and should be deleted on nrecover.";
			}
		}

		public void deinstrument(Counter counter, string filename)
		{
			string deinstFilename = filename + _extention;

			if (!File.Exists(deinstFilename) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56) && NCover.NCoverCheck.Add(56))
			{
				//Deinstrumented file does not exist, so assume someone has
				//previously done the deinstrumentation and that they have run this in 
				//error. Do nothing with this file.

				//unless the file never existed in the first place!
				if (File.Exists(filename + _shouldntExistExtention) && NCover.NCoverCheck.Add(58) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57) && NCover.NCoverCheck.Add(57))
				{
					File.Delete(filename);
					File.Delete(filename + _shouldntExistExtention);
				}

				return;
			}

			File.Delete(filename);
			File.Move(deinstFilename, filename);
		}

		#endregion
	}
}














